<?php
/*
 * Framework有两大功能，1、自动加载类，2、请求分发
 */
class Framework{
    //运行项目
    public static function run(){
        self::initConst();
        self::initConfig();
        self::initError();
        self::initRoutes();
        self::initRunningConst();
        self::initAutoLoad();
        self::initDispatch();
    }
    //1、定义路径常量
    private static function initConst(){
        define('DS', DIRECTORY_SEPARATOR);  //定义目录分割符
        define('ROOT_PATH', getcwd().DS);   //根目录
        define('APP_PATH', ROOT_PATH.'app'.DS);
        define('FRAMEWORK_PATH', ROOT_PATH.'framework'.DS);
        define('PUBLIC_PATH', ROOT_PATH.'public'.DS);
        define('CONFIG_PATH',APP_PATH.'config'.DS);
        define('CONTROLLER_PATH', APP_PATH.'controller'.DS);
        define('MODEL_PATH', APP_PATH.'model'.DS);
        define('VIEW_PATH', APP_PATH.'view'.DS);
        define('CORE_PATH', FRAMEWORK_PATH.'core'.DS);
        define('HELP_PATH', FRAMEWORK_PATH.'help'.DS);
        define('LIB_PATH', FRAMEWORK_PATH.'lib'.DS);
    }
    //2、导入配置文件,将配置文件的信息导入到全局变量config中
    private static function initConfig(){
        $GLOBALS['config']=require CONFIG_PATH.'config.php';
    }
    
    //3、确定路由
    private static function initRoutes(){
        $p=isset($_REQUEST['p'])?$_REQUEST['p']:$GLOBALS['config']['app']['default_platform'];
        $c=isset($_REQUEST['c'])?$_REQUEST['c']:$GLOBALS['config']['app']['default_controller'];
        $a=isset($_REQUEST['a'])?$_REQUEST['a']:$GLOBALS['config']['app']['default_action'];
        define('PLATFORM', $p);     //当前平台名称
        define('CONTROLLER', $c);   //当前控制器名称
        define('ACTION', $a);       //当前控制器方法
        define('__URL__', CONTROLLER_PATH.PLATFORM.DS);  //当前控制器文件所在的文件夹
        define('__VIEW__', VIEW_PATH.PLATFORM.DS);      //当前视图所在的文件夹
    }
    //初始化运行时常量
    private static function initRunningConst(){
        $upload=implode('|',$GLOBALS['config']['admin']['upload_type']);
        define('UPLOAD_TYPE',$upload);  //定义上传类别常量
        define('UPLOAD_SIZE',$GLOBALS['config']['admin']['upload_size']*1024);
        define('UPLOAD_PATH',$GLOBALS['config']['admin']['upload_path']);
        
    }
    //加载类的方法
    private static function autoLoad($class_name){
        $class_map=array(
            'MySQLDB'       =>  CORE_PATH.'MySQLDB.class.php',
            'Controller'    =>  CORE_PATH.'Controller.class.php',
            'Model'         =>  CORE_PATH.'Model.class.php',
        );
        if(isset($class_map[$class_name])){
            require $class_map[$class_name];
        }
        elseif(substr($class_name, -5)=='Model'){
            require MODEL_PATH.$class_name.'.class.php';
        }
        elseif(substr($class_name, -10)=='Controller'){
            require __URL__.$class_name.'.class.php';
        }
        elseif(substr($class_name, -3)=='Lib'){
            require LIB_PATH.$class_name.'.class.php';
        }
    }
    //4、注册加载类，让加载类的方法自动执行
    private static function initAutoLoad(){
        spl_autoload_register('self::autoLoad');
    }
    //5、请求分发
    private static function initDispatch(){
        $controller_name=CONTROLLER.'Controller';
        $action_name=ACTION.'Action';
        $obj=new $controller_name();
        $obj->$action_name();
    }
    //开启调试或运行模式
    private static function initError(){
        ini_set('error_reporting', E_ALL | E_STRICT);   //报所有的错
        if($GLOBALS['config']['app']['app_debug']){     //调试模式
            ini_set('display_errors', 'On');            //开启错误显示在浏览器上
            ini_set('log_errors', 'off');               //关闭日志功能
        }else{
            ini_set('display_errors', 'off');            //关闭开启错误显示在浏览器上
            ini_set('log_errors', 'on');               //开启日志功能
            ini_set('error_log', APP_PATH.'log'.DS.'error.log'); //日志保存的地址
        }
    }

}


